package com.ruoyi.project.dict.chengyu.controller;

import com.ruoyi.common.utils.JsonUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.dict.chengyu.domain.Chengyu;
import com.ruoyi.project.dict.chengyu.service.IChengyuService;
import com.ruoyi.project.dict.chengyutag.domain.ChengyuTag;
import com.ruoyi.project.dict.chengyutag.service.IChengyuTagService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 成语词典Controller
 *
 * @author shixiangwen
 * @date 2021-12-02
 */
@Controller
@RequestMapping("/dict/chengyu")
public class ChengyuController extends BaseController {

    private String prefix = "dict/chengyu";

    @Autowired
    private IChengyuService chengyuService;

    @Autowired
    private IChengyuTagService chengyuTagService;

    @RequiresPermissions("dict:chengyu:view")
    @GetMapping()
    public String chengyu() {
        return prefix + "/chengyu";
    }

    /**
     * 查询成语词典列表
     */
    @RequiresPermissions("dict:chengyu:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Chengyu chengyu) {
        startPage();
        List<Chengyu> list = chengyuService.selectChengyuList(chengyu);
        return getDataTable(list);
    }

    /**
     * 导出成语词典列表
     */
    @RequiresPermissions("dict:chengyu:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Chengyu chengyu) {
        List<Chengyu> list = chengyuService.selectChengyuList(chengyu);
        ExcelUtil<Chengyu> util = new ExcelUtil<Chengyu>(Chengyu.class);
        return util.exportExcel(list, "chengyu");
    }

    /**
     * 新增成语词典
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存成语词典
     */
    @RequiresPermissions("dict:chengyu:add")
    @Log(title = "成语词典", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Chengyu chengyu) {
        return toAjax(chengyuService.insertChengyu(chengyu));
    }

    /**
     * 修改成语词典
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        Chengyu chengyu = chengyuService.selectChengyuById(id);

        List<String> chengyuTags = null;
        if (!StringUtils.isEmpty(chengyu.getTag())) {
            chengyuTags = JsonUtil.toListObject(chengyu.getTag(), String.class);
        } else {
            chengyuTags = new ArrayList<>();
        }

        List<ChengyuTag> tags = chengyuTagService.selectChengyuTagList(new ChengyuTag());
        for (ChengyuTag tag : tags) {
            if (chengyuTags.contains(tag.getName())) {
                tag.setFlag(true);
            }
        }
        mmap.put("tags", tags);


        mmap.put("chengyu", chengyu);
        return prefix + "/edit";
    }

    @GetMapping("/editExtend/{id}")
    public String editExtend(@PathVariable("id") Long id, ModelMap mmap) {
        Chengyu chengyu = chengyuService.selectChengyuById(id);
        mmap.put("chengyu", chengyu);
        return prefix + "/diangu";
    }

    /**
     * 修改保存成语词典
     */
    @RequiresPermissions("dict:chengyu:edit")
    @Log(title = "成语词典", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Chengyu chengyu) {
        return toAjax(chengyuService.updateChengyu(chengyu));
    }

    /**
     * 删除成语词典
     */
    @RequiresPermissions("dict:chengyu:remove")
    @Log(title = "成语词典", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(chengyuService.deleteChengyuByIds(ids));
    }
}
